package com.imagepreview.plugin;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;

import java.util.ArrayList;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class RouterPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware {
    // 常量定义
    private static final String CHANNEL_NAME = "com.imagepreview.plugin/router_channel";
    private static final String METHOD_GET_IMAGES = "getImages";
    private static final String METHOD_GET_INDEX = "getIndex";
    private static final String METHOD_GET_ROUTE = "getRoute";
    private static final String EXTRA_IMAGES = "images";
    private static final String EXTRA_INDEX = "index";
    private static final String EXTRA_ROUTE = FlutterEngineDelegate.ROUTE_PATH;

    private MethodChannel channel;
    private Activity activity;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), CHANNEL_NAME);
        channel.setMethodCallHandler(this);
        Log.d("RouterPlugin", "onAttachedToEngine ----- > ..(^_^)..");
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        Log.d("RouterPlugin", "onMethodCall: " + call.method);
        if (METHOD_GET_ROUTE.equals(call.method)) {
            Intent intent = activity.getIntent();
            if (intent != null && intent.hasExtra(EXTRA_ROUTE)) {
                String route = intent.getStringExtra(EXTRA_ROUTE);
                Log.d("RouterPlugin", "getRoute: " + route);
                result.success(route);
            }
        } else if (METHOD_GET_IMAGES.equals(call.method)) {
            Intent intent = activity.getIntent();
            if (intent != null && intent.hasExtra(EXTRA_IMAGES)) {
                ArrayList<String> initialData = intent.getStringArrayListExtra(EXTRA_IMAGES);
                Log.d("RouterPlugin", "getImages: " + (initialData != null ? initialData.size() : 0));
                result.success(initialData);
            } else {
                Log.w("RouterPlugin", "Missing 'images' extra in intent");
                result.error("ERROR", "Missing 'images' extra in intent", null);
            }
        } else if (METHOD_GET_INDEX.equals(call.method)) {
            Intent intent = activity.getIntent();
            if (intent != null && intent.hasExtra(EXTRA_INDEX)) {
                int index = intent.getIntExtra(EXTRA_INDEX, 0);
                Log.d("RouterPlugin", "getIndex: " + index);
                result.success(index);
            } else {
                Log.w("RouterPlugin", "Missing 'index' extra in intent");
                result.error("ERROR", "Missing 'index' extra in intent", null);
            }
        } else {
            result.notImplemented();
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
        channel = null;
        activity = null;
        Log.d("RouterPlugin", "onDetachedFromEngine ----- > ..(~_~)..");
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        this.activity = binding.getActivity();
        Log.d("RouterPlugin", "onAttachedToActivity ----- > ..(^_^).. activity：" + activity);
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        onDetachedFromActivity();
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        onAttachedToActivity(binding);
    }

    @Override
    public void onDetachedFromActivity() {
        this.activity = null;
        Log.d("RouterPlugin", "onDetachedFromActivity ----- > ..(~_~)..");
    }
}